#include <iostream>
#include <chrono>
#include <vector>
#include <random>
#include <iomanip>

class MatrixSpeedTest {
private:
    std::vector<std::vector<double>> matrix;
    static const int ROWS = 5000;  
    static const int COLS = 5000;  
    
public:
    MatrixSpeedTest() {
        std::cout << "正在初始化 " << ROWS << " × " << COLS << " 矩阵 " 
                  << std::endl;
        
        // 初始化矩阵
        matrix.resize(ROWS, std::vector<double>(COLS));
        
        // 使用随机数填充矩阵
        std::random_device rd;
        std::mt19937 gen(rd());
        std::uniform_real_distribution<> dis(0.0, 100.0);
        
        // 初始化所有数据，确保测试准确性
        for (int i = 0; i < ROWS; ++i) {
            for (int j = 0; j < COLS; ++j) {
                matrix[i][j] = dis(gen);
            }
        }
        std::cout << "矩阵初始化完成" << std::endl;
    }
    
    // 按行读取测试
    double testRowWise(double& sumResult) {
        auto start = std::chrono::high_resolution_clock::now();
        
        double sum = 0.0;
        for (int i = 0; i < ROWS; ++i) {
            for (int j = 0; j < COLS; ++j) {
                sum += matrix[i][j];  // 按行访问
            }
        }
        
        auto end = std::chrono::high_resolution_clock::now();
        sumResult = sum;  // 保存求和结果
        return std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
    }
    
    // 按列读取测试
    double testColumnWise(double& sumResult) {
        auto start = std::chrono::high_resolution_clock::now();
        
        double sum = 0.0;
        for (int j = 0; j < COLS; ++j) {
            for (int i = 0; i < ROWS; ++i) {
                sum += matrix[i][j];  // 按列访问
            }
        }
        
        auto end = std::chrono::high_resolution_clock::now();
        sumResult = sum;  // 保存求和结果
        return std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
    }
    
    void runTests() {
        std::cout << "\n=== 大矩阵访问性能测试 (" << ROWS << "×" << COLS << ") ===" << std::endl;
        std::cout << "总元素数: " << ROWS * COLS  << std::endl;
        
        double rowSum, colSum;
        
        // 正式测试
        std::cout << "\n开始按行读取测试..." << std::endl;
        double rowTime = testRowWise(rowSum);
        
        std::cout << "开始按列读取测试..." << std::endl;
        double colTime = testColumnWise(colSum);
        
        // 显示结果
        std::cout << "\n=== 测试结果 ===" << std::endl;
        std::cout << std::fixed << std::setprecision(2);
        std::cout << "按行读取时间: " << rowTime << " 毫秒" << std::endl;
        std::cout << "按行读取求和: " << rowSum << std::endl;
        std::cout << "按列读取时间: " << colTime << " 毫秒" << std::endl;
        std::cout << "按列读取求和: " << colSum << std::endl;
        
        
        double speedup = colTime / rowTime;
        std::cout << "\n测试结果: 按行读取比按列读取快 " << speedup << " 倍" << std::endl;
    }
        
};

int main() {
    std::cout << "大矩阵缓存性能对比测试" << std::endl;
    std::cout << "=====================" << std::endl;
    
    
    MatrixSpeedTest test;
    test.runTests();
    
    return 0;
}